<template>
  <div class="app-container">
    <el-row>
      <el-col :span="13">
        <div class="grid-content bg-purple-dark">
          <el-card class="box-card">
            <div slot="header" class="clearfix">
              <span>新增二维码</span>
            </div>
            <div class="text item">
              <el-form ref="elForm" size="medium" label-width="100px">
                <el-col :span="12">
                  <el-form-item label="执法单位" prop="unit">
                    <el-input
                      v-model.trim="inputUnitName"
                      placeholder="请输入中文"
                      clearable
                      :style="{ width: '100%' }"
                      @blur="inputChange($event)"
                    >
                    </el-input>
                  </el-form-item>
                </el-col>
                <el-col :span="12">
                  <el-form-item label="生成数量">
                    <el-input-number
                      type="number"
                      :min="1"
                      :max="200"
                      controls-position="right"
                      v-model.trim="inputSerialNumber"
                    ></el-input-number>
                  </el-form-item>
                </el-col>
                <el-button
                  :loading="loading"
                  type="primary"
                  size="small"
                  class="add-qrcode-btn"
                  @click="addQrcode"
                  >新增</el-button
                >
              </el-form>
            </div>
          </el-card>
        </div>
      </el-col>
      <el-col v-if="downSerialNumber" class="show-qrcode" :span="6" :offset="2">
        <el-card class="box-card">
          <div slot="header" class="clearfix">
            <span>{{ downSerialNumber }}</span>
          </div>
          <div class="text item" style="text-align: center">
            <div>
              <image-preview :src="qrcode" v-if="qrcode" class="addQrcode"></image-preview>
              <br />
              <el-button type="primary" class="d_btn" size="small" @click="downQrcodeLocality"
                >下载</el-button
              >
              <br />
              <el-button type="primary" class="d_btn" size="small" @click="downQrcodePdf"
                >排版到纸质上</el-button
              >
            </div>
          </div>
        </el-card>
      </el-col>
      <el-col :span="13" style="margin-top: 10px" class="qrcode-list">
        <el-card class="box-card">
          <div slot="header" class="clearfix">
            <span>最近建码</span>
            <el-button style="float: right; padding: 3px 0" type="text">{{
              total === 0 ? '暂无数据' : `共${total}条`
            }}</el-button>
          </div>
          <div v-for="(item, i) in List" :key="i" class="text item list">
            <div class="item-c" v-if="item.bindStatus === '1'">
              <div class="c-left">
                <QrCodeImagePreview :src="item.imgUrl" class="qrcode"></QrCodeImagePreview>
              </div>
              <div class="c-right">
                <div class="right-top">{{ item.serialNum }}</div>
                <div class="right-bottom">
                  <div>创建时间: {{ item.createTime }}</div>
                  <div>
                    <span
                      type="default"
                      size="small"
                      @click="downQrcode(item.serialNum, item.imgUrl)"
                      >下载二维码</span
                    ><span
                      type="default"
                      size="small"
                      @click="
                        ;(dialogVisible = true),
                          (delSerialNumber = item.serialNum),
                          (delQrId = item.id)
                      "
                      >删除</span
                    >
                  </div>
                </div>
              </div>
              <!-- 是否删除弹框 -->
              <el-dialog
                title="提示"
                :visible.sync="dialogVisible"
                width="30%"
                :before-close="handleClose"
              >
                <span>确定删除 {{ delSerialNumber }}吗 ?</span>
                <span slot="footer" class="dialog-footer">
                  <el-button @click="dialogVisible = false">取 消</el-button>
                  <el-button type="primary" @click="delQrcode">确 定</el-button>
                </span>
              </el-dialog>
            </div>
          </div>
        </el-card>
      </el-col>
    </el-row>
    <Pagination
      class="page-container"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="pageGetList"
    ></Pagination>
  </div>
</template>

<script>
import { getNotBIndList, addQrcode, delQrcode, getLists } from '@/api/property/qrcode/index.js'
import { getInitial } from '@/utils/Pinyin'
import QrCodeImagePreview from './components/qrcode-image-poreview.vue'
let that
export default {
  name: 'Qrcode',
  components: {
    QrCodeImagePreview
  },
  data() {
    return {
      // 输入的执法单位
      inputUnitName: '',
      // 执法单位首字母
      UnitNameInitia: '',
      // 生成二维码的数量
      inputSerialNumber: '',
      // 总条数
      total: 0,
      // 查询分页表格数据
      List: [],
      // 点击下载的二维码序列
      downSerialNumber: null,
      // 点击下载的二维码图片
      qrcode: '',
      // 删除二维码弹框
      dialogVisible: false,
      // 删除的二维码序列
      delSerialNumber: '',
      // 删除的二维码id,
      delQrId: '',
      // 按钮是否加载
      loading: false,
      // 分页查询
      queryParams: {
        bindStatus: '1',
        pageNum: 1,
        pageSize: 10
      }
    }
  },
  created() {
    this.getList()
    that = this
  },
  mounted() {},
  methods: {
    /** 获取最近建码 */
    getList() {
      getNotBIndList().then((res) => {
        this.List = res.rows
        this.total = res.total
      })
    },
    // 分页查询
    pageGetList() {
      getLists(this.queryParams).then((response) => {
        this.List = response.rows
        this.total = response.total
      })
    },
    // 只能输入中文限制
    inputChange(e) {
      const o = e.target
      o.value = o.value.replace(/[^\u4E00-\u9FA5]/g, '') // 清除除了中文以外的输入的字符
      this.inputUnitName = o.value
    },
    /** 新增按钮操作 */
    addQrcode() {
      if (this.inputUnitName.length < 2) {
        this.$modal.msgWarning('执法单位最少为2位 !')
        return
      }
      this.loading = true
      const params = {
        prefix: this.UnitNameInitia.substring(0, 2),
        count: this.inputSerialNumber
      }
      addQrcode(params).then((res) => {
        this.getList()
        this.$modal.msgSuccess('新增成功')
        this.inputSerialNumber = ''
        this.inputUnitName = ''
        this.loading = false
      })
    },
    // 最近建码中的下载二维码
    downQrcode(code, img) {
      this.downSerialNumber = code
      this.qrcode = img
    },
    // 删除二维码
    delQrcode() {
      delQrcode(this.delQrId).then((res) => {
        this.dialogVisible = false
        // 如果删除的二维码序列被选中了 就清空
        if (this.delSerialNumber === this.downSerialNumber) {
          this.downSerialNumber = null
        }
        this.getList()
        this.$modal.msgSuccess('删除成功')
      })
    },
    handleClose() {
      this.dialogVisible = false
    },
    // 下载二维码图片到本地
    downQrcodeLocality() {
      this.$download.resource(this.qrcode, this.downSerialNumber)
    },
    // 下载pdf文件到本地
    downQrcodePdf() {
      const routeData = this.$router.resolve({
        path: '/printPdf', //路由path
        // 传参
        query: {
          url: `${process.env.VUE_APP_BASE_API}${this.qrcode}`
        }
      })
      // 跳转到打印页面
      window.open(routeData.href, '_blank')
    }
  },
  watch: {
    inputUnitName: (newData, oldData) => {
      that.UnitNameInitia = getInitial(newData)
    }
  }
}
</script>
<style scoped lang="scss">
.c-left {
  height: 100px;
}

.add-qrcode-btn {
  width: 100px;
  float: right;
  margin-right: 48px;
  margin-bottom: 20px;
}
.qrcode {
  width: 100px;
}
.d_btn {
  margin: 8px 0;
  width: 160px;
}
.show-qrcode {
  position: absolute;
  right: 100px;
}
::v-deep .grid-content {
  .el-card__body {
    padding-left: 0;
    padding-right: 0;
  }
}
::v-deep .qrcode-list .el-card {
  padding: 0;
  height: calc(100vh - 84px - 170px - 10px - 40px - 50px);

  overflow-y: auto;
  .el-card__body {
    padding: 0;
  }
}
.list:not(:first-child) {
  border-top: 1px solid black;
}
.item-c {
  padding: 15px;
  display: flex;
  .c-right {
    padding: 15px;
    flex: 1;
    display: flex;
    flex-direction: column;
    justify-content: space-between;
    .right-bottom {
      display: flex;
      justify-content: space-between;
      div {
        flex: 1;
        span {
          float: right;
          margin-right: 20px;
          cursor: pointer;
        }
      }
    }
  }
}
::v-deep .pagination-container .el-pagination {
  position: absolute;
  left: 0 !important;
}
.addQrcode {
  width: 160px;
}
</style>
